package cn.kmsoft.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 * Joda时间处理器-代码来源于网络
 * 
 * @author cqwensten 2020-08-05
 *  email:cqwensten@163.com 
 *
 */
public class JodaDateTimeUtil {
	//标准时间格式定义
	//"Thu Sep 24 00:00:00 CST 2020"
	public static final String CST_DateTimeFORMAT = "EEE MMM dd HH:mm:ss zzz yyyy";
	public static final String GMT_DateTimeFORMAT = "EEE MMM dd hh:mm:ss z yyyy";
	public static final String STANDARD_DateTimeFORMAT="yyyy-MM-dd HH:mm:ss";
	
	public static final String STANDARD_DateFORMAT="yyyy-MM-dd";
 
    /**
     * strToDate将指定的字符串转化成指定格式的日期
     * 
     * @param dateTimeStr
     * @return
     */
    public static Date strToDate(String dateTimeStr){
		Date date = null;
		try {
			if (dateTimeStr.toUpperCase().contains("CST")) {
    			//Date date = new Date();
    			SimpleDateFormat sdf = new SimpleDateFormat(CST_DateTimeFORMAT, Locale.US);
    			date = sdf.parse(dateTimeStr);
    		} 
			else if(dateTimeStr.toUpperCase().contains("GMT")){
    			SimpleDateFormat sdf = new SimpleDateFormat(CST_DateTimeFORMAT, Locale.US);
    			date = sdf.parse(dateTimeStr);
    		}
			else if(dateTimeStr.toUpperCase().contains("T")){
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    			date = df.parse(dateTimeStr);
    		}
    		else {
    			SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_DateTimeFORMAT);
    			date = sdf.parse(dateTimeStr);
    		}
		} catch (ParseException e) {
			e.printStackTrace();
		}
    	
        return date;
    }
    
    /**
     * formatToStdDateTime-将时间字符串dateTimeStr格式化为 标准的yyyy-MM-dd HH:mm:ss格式
     * 
     * @param dateTimeStr 时间字符串，包含CST、GMT和std
     * @return
     */
    public static String formatDateTime(String dateTimeStr) {
    	String formatVal = null;
    	if(dateTimeStr.toUpperCase().contains("CST")) {									
			SimpleDateFormat sdf = new SimpleDateFormat(CST_DateTimeFORMAT, Locale.US);
			try {
				Date date = sdf.parse(dateTimeStr);
				//转换为标准STANDARD_DateTimeFORMAT格式
				formatVal = new SimpleDateFormat(STANDARD_DateTimeFORMAT).format(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}else if(dateTimeStr.toUpperCase().contains("GMT")) {
		    try {
		    	SimpleDateFormat sdf = new SimpleDateFormat(GMT_DateTimeFORMAT, Locale.US);
		        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
				Date date = sdf.parse(dateTimeStr);
				formatVal = new SimpleDateFormat(STANDARD_DateTimeFORMAT).format(date);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if(dateTimeStr.toUpperCase().contains("T")) {
		    try {
		    	//2020-11-02T21:55:53.139+08:00
		    	DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	            Date date = df.parse(dateTimeStr);
				formatVal = new SimpleDateFormat(STANDARD_DateTimeFORMAT).format(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
    	else {
    		try {
		    	SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_DateTimeFORMAT);
				Date date = sdf.parse(dateTimeStr);
				formatVal = new SimpleDateFormat(STANDARD_DateTimeFORMAT).format(date);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    	
    	return formatVal;
    }
    
    /**
     * formatDate-将时间字符串dateTimeStr格式化为 标准的yyyy-MM-dd格式
     * 
     * @param dateTimeStr 时间字符串，包含CST、GMT和std
     * @return
     */
    public static String formatDate(String dateTimeStr) {
    	String formatVal = null;
    	if(dateTimeStr.toUpperCase().contains("CST")) {									
			SimpleDateFormat sdf = new SimpleDateFormat(CST_DateTimeFORMAT, Locale.US);
			try {
				Date date = sdf.parse(dateTimeStr);
				//转换为标准STANDARD_DateTimeFORMAT格式
				formatVal = new SimpleDateFormat(STANDARD_DateFORMAT).format(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}else if(dateTimeStr.toUpperCase().contains("GMT")) {
		    try {
		    	SimpleDateFormat sdf = new SimpleDateFormat(GMT_DateTimeFORMAT, Locale.US);
		        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
				Date date = sdf.parse(dateTimeStr);
				formatVal = new SimpleDateFormat(STANDARD_DateFORMAT).format(date);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if(dateTimeStr.toUpperCase().contains("T")) {
		    try {
		    	//2020-11-02T21:55:53.139+08:00
		    	DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	            Date date = df.parse(dateTimeStr);
				formatVal = new SimpleDateFormat(STANDARD_DateFORMAT).format(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
    	else {
    		try {
		    	SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_DateTimeFORMAT);
				Date date = sdf.parse(dateTimeStr);
				formatVal = new SimpleDateFormat(STANDARD_DateFORMAT).format(date);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    	
    	return formatVal;
    }
    
    
    /**
     * strToDateTime-解析dateTimeStr将时间字符串成标准格式的时间
     * 
     * @param dateTimeStr 可能是标准的，有可能是cst格式
     * @return
     */
    public static DateTime strToDateTime(String dateTimeStr){
    	String dateTimeStr_std = null;
    	if(dateTimeStr.toUpperCase().contains("CST")) {
			try {
				SimpleDateFormat sdf = new SimpleDateFormat(CST_DateTimeFORMAT, Locale.US);
				Date date = sdf.parse(dateTimeStr);
				//转换为标准STANDARD_DateTimeFORMAT格式
				dateTimeStr_std = new SimpleDateFormat(STANDARD_DateTimeFORMAT).format(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}else if(dateTimeStr.toUpperCase().contains("GMT")) {
		    try {
		    	SimpleDateFormat sdf = new SimpleDateFormat(GMT_DateTimeFORMAT, Locale.US);
		        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
				Date date = sdf.parse(dateTimeStr);
				//转换为标准STANDARD_DateTimeFORMAT格式
				dateTimeStr_std =  new SimpleDateFormat(STANDARD_DateTimeFORMAT).format(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		else if(dateTimeStr.toUpperCase().contains("T")) {
		    try {
		    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
				Date date = sdf.parse(dateTimeStr);
				//转换为标准STANDARD_DateTimeFORMAT格式
				dateTimeStr_std =  new SimpleDateFormat(STANDARD_DateTimeFORMAT).format(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
    	else {
    		dateTimeStr_std = dateTimeStr;
		}
    	
    	DateTime dtimeVal_std = JodaDateTimeUtil.strToDateTime_std(dateTimeStr_std);
        return dtimeVal_std;
    }
    public static DateTime strToDateTime_std(String dateTimeStr_std){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_DateTimeFORMAT);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr_std);
        return dateTime;
    }
   
    /**
     * dateToStr 将日期转化成指定格式的日期字符串
     * 
     * @param date
     * @param formatStr 不输入，则默认yyyy-MM-dd格式
     * @return
     */
    public static String dateToStr(Date date,String formatStr){
        if(date==null){
            return "";
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatStr);
    }
    public static String dateToStr(Date date){
        if(date==null){
            return "";
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(STANDARD_DateFORMAT);
    }
    
    /**
     * dateTimeToStr 将时间转化成指定格式的时间字符串
     * 
     * @param dTimeStr
     * @param formatStr 不输入，则默认yyyy-MM-dd HH:mm:ss格式
     * 
     * @return
     */
    public static String dateTimeToStr(DateTime dTimeStr,String formatStr){
        if(dTimeStr==null){
            return "";
        }
        DateTime dateTime = new DateTime(dTimeStr);
        return dateTime.toString(formatStr);
    }
    public static String dateTimeToStr(DateTime dTimeStr){
        if(dTimeStr==null){
            return "";
        }
        DateTime dateTime = new DateTime(dTimeStr);
        return dateTime.toString(STANDARD_DateTimeFORMAT);
    }
    

    public static void main(String[] args) {
    	//Joda Time
    	
    	DateTime dtimeNow1= new DateTime();
    	String dateTimeStr_t = dtimeNow1.toString();
    	System.out.println("1: "+dateTimeStr_t);   	
    	String dateTimeStr_std1 =	JodaDateTimeUtil.formatDateTime(dateTimeStr_t);
    	String dateTimeStr_std2 =	JodaDateTimeUtil.formatDate(dateTimeStr_t);
    	System.out.println("1-1: "+dateTimeStr_std1);
    	System.out.println("1-2: "+dateTimeStr_std2);
    	
    	System.out.println("2: "+dtimeNow1.toString( JodaDateTimeUtil.STANDARD_DateFORMAT ));
    	System.out.println("3: "+dtimeNow1.toString( JodaDateTimeUtil.STANDARD_DateTimeFORMAT ));
    	
    	String dateTime_cst="Thu Sep 24 00:00:00 CST 2020";
    	DateTime dtime1 = JodaDateTimeUtil.strToDateTime(dateTime_cst);
    	System.out.println("1: "+dtime1.toString());
    	System.out.println("2: "+dtime1.toString( JodaDateTimeUtil.STANDARD_DateFORMAT ));
    	System.out.println("3: "+dtime1.toString( JodaDateTimeUtil.STANDARD_DateTimeFORMAT ));
    	Date dt = JodaDateTimeUtil.strToDate(dateTime_cst);
    	System.out.println("4: "+dt.toString());
    	
    	String dateTimeStr_std= JodaDateTimeUtil.formatDateTime(dateTime_cst);
    	System.out.println("5: "+dateTimeStr_std);
    	
    	//java date
    	Date dateNow = new Date();
    	System.out.println("1: "+dateNow.toString());
    	System.out.println("2: "+dateNow.toLocaleString());
    	System.out.println("3: "+dateNow.toGMTString());
    	System.out.println("4: "+JodaDateTimeUtil.dateToStr(dateNow, JodaDateTimeUtil.STANDARD_DateFORMAT ));
        
        DateTime dtimeNow = new DateTime();
        System.out.println(JodaDateTimeUtil.dateTimeToStr(dtimeNow, JodaDateTimeUtil.CST_DateTimeFORMAT ));
        System.out.println(JodaDateTimeUtil.dateTimeToStr(dtimeNow, JodaDateTimeUtil.STANDARD_DateTimeFORMAT ));
        System.out.println(JodaDateTimeUtil.dateTimeToStr(dtimeNow));

        System.out.println("--------重载方法测试---------");
        System.out.println(JodaDateTimeUtil.dateToStr(new Date()));
        String dateTimeStr = "2019-04-10 08:53:05";
        DateTime dtime2 = JodaDateTimeUtil.strToDateTime(dateTimeStr);
        System.out.println(JodaDateTimeUtil.dateTimeToStr(dtime2));
    }
    

}
